package com.plian.system.controller.pm.propertytransaction;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.FileTools;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.TokenUtil;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.entity.pm.propertytransaction.PropertyTransaction;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.pm.propertytransaction.IPropertyTransactionService;
import com.plian.system.service.pm.publiclist.IPublicListService;
import com.plian.system.service.pm.subjectEvaluation.ISubjectEvaluationService;
import com.plian.system.service.sys.file.FileService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.pm.propertytransaction.PropertyTransactionVO;
import com.plian.system.wrapper.pm.propertytransaction.PropertyTransactionWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 产权交易备案主表 控制器
 */
@Api(value = "产权交易备案主表", tags = "产权交易备案主表 控制器")
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("api/propertytransaction")
public class PropertyTransactionController extends BaseController {

    private IPropertyTransactionService propertyTransactionService;
    private ISubjectEvaluationService subjectEvaluationService;
    private IPublicListService publicListService;
    private FileService fileService;
    private FormWorkflowService formWorkflowService;

    private PropertyTransactionWrapper propertyTransactionWrapper;

    /**
     * 详情
     */
    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入propertyTransaction")
    public R<PropertyTransactionVO> detail(@Valid @RequestBody PropertyTransaction propertyTransaction) {
        PropertyTransactionVO propertyTransactionVO = propertyTransactionWrapper.entityToVO(propertyTransactionService.getOne(Condition.getQueryWrapper(propertyTransaction)));
        return propertyTransactionService.getDetailObj(propertyTransactionVO);
    }

    /**
     * 分页
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询", notes = "传入query")
    public ListResult<PropertyTransactionVO> list(@Valid @RequestBody Query query) {
        if (query.getAscs() == null && query.getDescs() == null) {
            query.setDescs("create_time");
//			query.setAscs("code");
        }

        List<SearchEntity> entityList = query.getSearch();
        QueryWrapper<PropertyTransaction> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new PropertyTransaction()), entityList
        );
        queryWrapper.eq("create_company_id", TokenUtil.getTokenOrgId());
        IPage<PropertyTransaction> pages = propertyTransactionService.page(Condition.getPage(query), queryWrapper);

        List<PropertyTransactionVO> propertyTransactionVOS = propertyTransactionWrapper.entityToVO(pages.getRecords());
        ListResult<PropertyTransactionVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(propertyTransactionVOS);
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        return listResult;
    }

    /**
     * 分页
     */
    @PostMapping("/reportList")
    @ApiOperation(value = "分页查询", notes = "传入pd")
    public ListResult<PageData> reportList(@RequestBody PageData pd) {
        return propertyTransactionService.reportList(pd);
    }


    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @LogOperation("产权交易备案-提交")
    @ApiOperation(value = "新增或修改", notes = "传入propertyTransactionVO")
    public ObjResult submit(@Valid @RequestBody PropertyTransactionVO propertyTransactionVO) {
        return propertyTransactionService.submit(propertyTransactionVO);
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @LogOperation("产权交易备案-删除")
    @ApiOperation(value = "删除", notes = "传入pd")
    public ObjResult remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>) pd.get("ids");
        return propertyTransactionService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA))) ? new ObjResult(CommonCode.SUCCESS) : new ObjResult(CommonCode.FAIL);
    }

    /**
     * 下载doc
     */
    @ApiOperation(value = "下载doc", notes = "传入id")
    @GetMapping("/buildword/{id}")
//    @PassToken
    public void buildword(@PathVariable String id, HttpServletResponse response) {
        propertyTransactionService.buildword(id, response);
    }

    /**
     * 打印
     */
    @GetMapping("/buildpdf/{id}")
    @ApiOperation(value = "打印", notes = "传入id")
//    @PassToken
    public ResponseEntity<FileSystemResource> buildPdf(@PathVariable Long id) throws Exception {
        //return FileTools.exportPdf(staffFilesService.buildpdf(id));
        return FileTools.exportPdf(propertyTransactionService.createReceptFile(id));
    }

    /**
     * 产权交易报表
     */
    @PostMapping("/propertytable")
    @ApiOperation(value = "产权交易报表", notes = "传入pd")
    public ObjResult propertyTable(@RequestBody PageData pd) {
        return propertyTransactionService.propertyTable(pd);
    }

}
